﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using BusinessLayer.WorldArtPrice.Models;

namespace BackEnd.Controllers
{
    public class AdminUserGroupController : Controller
    {
        //
        // GET: /AdminUserGroup/

        public ActionResult Index()
        {
            var subBL = new AdminUserGroupBL();
            var lstAdminUserGroups = new List<AdminUserGroup>();
            lstAdminUserGroups.AddRange(subBL.GetAll());

            return View("Index", lstAdminUserGroups);
        }
        //
        // GET:/AdminUserGroup/Create
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(AdminUserGroupModel model)
        {
            if (ModelState.IsValid)
            {
                var subsBL = new AdminUserGroupBL();
                var check = subsBL.GetByUserGroupCode(model.Code);
                if (check == null)
                {
                    var subs = new AdminUserGroup();
                    subs.Code = model.Code;
                    subs.Name = model.Name;
                    
                    int result = subsBL.CreateAdminUserGroup(subs);
                    if (result > 0)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ViewData["ErrorMessage"] = "Has an error when executing insert AdminUserGroup into database";
                    }
                }
                else
                {
                    ViewData["ErrorMessage"] = String.Format("This AdminUserGroup - {0} exists!", model.Code);

                }

            }

            return View(model);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(string id)
        {

            var subsBL = new AdminUserGroupBL();
            if (!String.IsNullOrEmpty(id))
            {
                List<string> lstOfSubsIDs = new List<string>();
                foreach (string item in id.Split('_'))
                {
                    int temp = -1;
                    Int32.TryParse(item, out temp);
                    if (temp > 0)
                    {
                        lstOfSubsIDs.Add(item);
                    }
                }
                int result = subsBL.DeleteAdminUserGroupByIds(lstOfSubsIDs);
                if (result > 0)
                {
                    //return Json(1, JsonRequestBehavior.AllowGet);
                    ViewData["ErrorMessage"] = "Delete successful!";
                    return RedirectToAction("Index");
                }

            }
            //return Json(0, JsonRequestBehavior.AllowGet);
            ViewData["ErrorMessage"] = "Cannot delete selected items!";
            return RedirectToAction("Index");


        }
        //
        // GET:/AdminUserGroup/Edit/id
        //[AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int id)
        {
            var subBL = new AdminUserGroupBL();
            var subs = subBL.GetByUserGroupID(id);
            var subModel = new AdminUserGroupModel();
            subModel.UserGroupID = subs.UserGroupID;
            subModel.Code = subs.Code;
            subModel.Name = subs.Name;

            return View("Edit", subModel);
        }
        [HttpPost]
        public ActionResult Edit(AdminUserGroupModel model)
        {
            if (ModelState.IsValid)
            {
                var subsBL = new AdminUserGroupBL();
                var check = subsBL.GetByUserGroupID(model.UserGroupID);
                if (check != null)
                {
                    var subs = new AdminUserGroup();
                    subs.UserGroupID = model.UserGroupID;
                    subs.Code = model.Code;
                    subs.Name = model.Name;

                    int result = subsBL.UpdateAdminUserGroup(subs);
                    if (result > 0)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ViewData["ErrorMessage"] = "Has an error when executing insert AdminUserGroup into database";
                    }
                }
                else
                {
                    ViewData["ErrorMessage"] = String.Format("This AdminUserGroup - {0} not exists!", model.Code);

                }

            }
            
            return View(model);
        }


    }
}
